package com.heatup.apt.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.heatup.apt.api.service.CoachCallUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;

/**
 *
 * CoachCallUser 控制层
 *
 */
@Controller
@RequestMapping("/coach_call_user")
public class CoachCallUserController extends BaseController{
	
	Logger logger =  LoggerFactory.getLogger(CoachCallUserController.class);
	
	@Autowired
	private CoachCallUserService coachCallUserService;
	
	@ResponseBody
	@RequestMapping("/call_user")
	public String recordCoachCallUser(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Integer orderId = RequestTools.RequestInt(request, "orderId", 0);
		UserInfo userInfo = this.getUserInfo();
		Map<String,Object> parmsMap = new HashMap<>();
		if(userInfo == null || this.getUserId() == null){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
			
		}
		
		if(orderId == 0){
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.ORDER_NOT_FIND.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.ORDER_NOT_FIND.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
			
		}
		
		try {
			parmsMap.put("coachUserInfo", userInfo);
			parmsMap.put("orderId", orderId);
			apiResultModel = coachCallUserService.saveCoachCallUser(parmsMap);
		} catch (Exception e) {
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setRet(apiResultModel.getErrorCode());
			logger.error("================ recordCoachCallUser error:"+e.toString()+e.getCause()+e);
		}
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel),callback);
	}
	
}